import logging
from flask import session
from flask_script import Manager
from flask_migrate import Migrate,MigrateCommand

from info import create_app,db,models
from info.models import User

#通过指定名字 创建对应的配置的 app
#create_app()传递来的函数

app = create_app('development')
manager=Manager(app)
#将app和db关联
Migrate(app,db)
#讲迁移命令添加到manager中
manager.add_command('db',MigrateCommand)

##########################################################
#创建命令行脚本

@manager.option("-n","-name",dest = 'name')
@manager.option("-p","-password",dest = 'password')
def createadmin(name,password):
    #创建管理员的帐户
    if not all([name, password]):
        print('参数不足')
        return
    user = User()
    user.nick_name = name
    user.password = password
    user.mobile = name
    #开启管理员的字段
    user.is_admin = True

    try:
        db.session.add(user)
        db.session.commit()
        print("创建成功")
    except Exception as e:
        print(e)
        db.session.rollback()

if __name__ == '__main__':
    print(app.url_map)#打印注册的路由
    manager.run()